﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using CCWin.SkinControl;
using CefSharp;
using CefSharp.WinForms;

namespace DC.Views
{
    public partial class Form_NewExample : Skin_DevExpress
    {
        public Form_NewExample()
        {
            InitializeComponent();
            InitializeChromium();
        }


        public string patientID = string.Empty;
        public string UserID = string.Empty;
        Utils.Database DB = Utils.Global.GlobalDB;

        private ChromiumWebBrowser chromeBrowser;

        private void InitializeChromium()
        {
            chromeBrowser = new ChromiumWebBrowser(Application.StartupPath + @"\Resources\Location\index.html");
            this.location_panel.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }

        private void Form_NewExample_Load(object sender, EventArgs e)
        {
            DataRow row = DB.GetDs("select User_ID,Real_Name from [User] where User_ID=" + UserID).Tables[0].DefaultView[0].Row;
            label12.Text = row["Real_Name"].ToString();
            DataRow row2 = DB.GetDs("select Patient_Name from Patient where Patient_ID=" + patientID).Tables[0].DefaultView[0].Row;
            label13.Text = row2["Patient_Name"].ToString();
            label14.Text = DateTime.Now.ToString();
        }
        private void skinButton1_Click(object sender, EventArgs e)
        {
            Task<JavascriptResponse> task = chromeBrowser.EvaluateScriptAsync("getLocations();");
            // 等待js 方法执行完后，获取返回值

            task.ContinueWith(t =>
            {
                if (!t.IsFaulted)
                {
                    // t.Result 是 CefSharp.JavascriptResponse 对象
                    // t.Result.Result 是一个 object 对象，来自js的 getLocations() 方法的返回值
                    string locations = t.Result.Result.ToString();
                    string sql = string.Format(@"insert into Example(Patient_ID, User_ID, Example_Time, Example_Main, Example_Past, Example_Allergic, 
                                                   Example_Now, Cure, Advice, Location, IsFirst)values({0},{1},'{2}','{3}','{4}','{5}','{6}','{7}',
                                                   '{8}','{9}',{10})",
                                                       patientID,
                                                       UserID,
                                                       label14.Text,
                                                       skinTextBox1.Text,
                                                       skinTextBox2.Text,
                                                       skinTextBox3.Text,
                                                       skinTextBox10.Text,
                                                       skinTextBox4.Text,
                                                       skinTextBox11.Text,
                                                       locations,
                                                       radioButton1.Checked ? "0" : "1"
                                                       );
                    DB.Execute(sql);
                    DB.Execute("update Patient set Patient_Last='" + label14.Text + "' where Patient_ID=" + patientID);
                    MessageBox.Show("保存病例成功！", "保存成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            Utils.InputBox box = new Utils.InputBox();
            box.Title = "请输入";
            box.MessageText = "输入新建的病历模板名称：";
            box.ShowDialog();
            if (box.ButtonClick != 1)
            {
                return;
            }
            string name = box.Content;
            if (name == string.Empty)
            {
                MessageBox.Show("请输入保存的病例模板名称！","出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int count = Convert.ToInt32(DB.GetData("select count (*) from Template where Template_Type='" + name + "'").ToString());
            if (count != 0)
            {
                MessageBox.Show("病例模板名称重复！", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string sql = string.Format(@"insert into Template(Template_Type,Template_Main, Template_Cure,
Template_Advice,Template_Past, Template_Allergic, Template_Now)values(
                                                '{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                                name,
                                                skinTextBox1.Text,
                                                skinTextBox4.Text,
                                                skinTextBox11.Text,
                                                skinTextBox2.Text,
                                                skinTextBox3.Text,
                                                skinTextBox10.Text
                                                );
            DB.Execute(sql);
            MessageBox.Show("保存病例模板成功！", "保存成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            skinTextBox2.Enabled = true;
            skinTextBox3.Enabled = true;
            skinTextBox10.Enabled = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            skinTextBox2.Text = string.Empty;
            skinTextBox3.Text = string.Empty;
            skinTextBox10.Text = string.Empty;
            skinTextBox2.Enabled = false;
            skinTextBox3.Enabled = false;
            skinTextBox10.Enabled = false;

        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            Views.Form_InsertTemplate form_InsertTemplate = new Views.Form_InsertTemplate();
            form_InsertTemplate.ShowDialog();
            if (form_InsertTemplate.TemplateID != string.Empty)
            {
                if (radioButton1.Checked)
                {
                    // 载入模版
                    string sql = "select * from Template where Template_ID=" + form_InsertTemplate.TemplateID;
                    DataView dv = DB.GetDs(sql).Tables[0].DefaultView;
                    skinTextBox1.Text = dv[0]["Template_Main"].ToString();
                    skinTextBox4.Text = dv[0]["Template_Cure"].ToString();
                    skinTextBox11.Text = dv[0]["Template_Advice"].ToString();
                    skinTextBox2.Text = dv[0]["Template_Past"].ToString();
                    skinTextBox3.Text = dv[0]["Template_Allergic"].ToString();
                    skinTextBox10.Text = dv[0]["Template_Now"].ToString();
                }
                if (radioButton2.Checked)
                {
                    skinTextBox2.Text= string.Empty;
                    skinTextBox3.Text = string.Empty;
                    skinTextBox10.Text = string.Empty;
                    string sql = "select * from Template where Template_ID=" + form_InsertTemplate.TemplateID;
                    DataView dv = DB.GetDs(sql).Tables[0].DefaultView;
                    skinTextBox1.Text = dv[0]["Template_Main"].ToString();
                    skinTextBox4.Text = dv[0]["Template_Cure"].ToString();
                    skinTextBox11.Text = dv[0]["Template_Advice"].ToString();
                }
            }
        }
    }

    public class Loc
    {
        public string l1;
        public string l2;
        public string l3;
        public string l4;
        public string check;
    }
}
